import {
	getUserInfo
} from '@/common/api/index.js'
import {
	showTishi
} from '@/utils/index.js'
import {
	API_URL,
	BASE_URL
} from '@/common/public_url.js'
import kdic from '@/utils/statusCode.js'
//  http://47.120.57.175/anonym/userauth/login/

					
export function handleLogin(username, password) {
	return new Promise((resolve, reject) => {
		uni.request({
			// url: `${API_URL}/anonym/userauth/login/`,
			url: `${API_URL}/anonym/token/`,
			timeout: 10000,
			method: 'POST',
			data: {
				username: username,
				password: password,
			},
			responseType: 'application/json',
			header: {
				"Content-Type": "application/json",
				// "Access-Control-Allow-Credentials":true,
				// "Authorization":"JWT "+token.refresh,
				// "Authorization":"JWT "+token.access,
				// "Authentication" :token.refresh,
			},
			//注意这个地方要开启 true才会自动获取cookie和自动传递cookie
			withCredentials:true,
			success: function(res) {
				console.log(res,'ressuccess');
				
				if(res.statusCode==200){
					// 保存请求头
					uni.setStorageSync('JWTtoken', res.data.access)
					uni.setStorageSync('JWTtoken—refresh', res.data.refresh)
					// 跳转到首页
					wx.switchTab({
						url: '/pages/student/index/index', // 替换为你的 tabBar 页面路径
						success: function(res) {
							// 跳转成功的回调
							console.log(res, '跳转成功')
						},
						fail: function(err) {
							// 错误处理
							console.log(err, '为什么不跳转')
						}
					}); 
				}else{
					uni.showToast({
						title: res.data.detail,
						icon: 'none'
					})
				}
				
				
			},
			fail: function(error) {
				showTishi(error)
				reject(error)
			},
			complete: function(res) {
				console.log(res, "complete22")
			},
		})
	})
}
// 请求验证码接口
export function handleqrcode(phoneNum){
	return new Promise((resolve, reject) => {
		uni.request({
			// url: `${API_URL}/anonym/userauth/login/`,
			url: `${API_URL}/anonym/userauth/login/`,
			timeout: 10000,
			method: 'POST',
			data: {
				phoneNum: phoneNum,
			},
			responseType: 'application/json',
			header: {
				"Content-Type": "application/json",
			},
			// withCredentials:true,
			success: function(res) {
				console.log(res,'ressuccess');
				if(res.statusCode==200){
					uni.showToast({
						title: '验证码发送成功！',
						icon: 'success'
					}); 
				}else{
					uni.showToast({
						title: res.data.detail,
						icon: 'none'
					})
				}
				
				
			},
			fail: function(error) {
				showTishi(error)
				reject(error)
			},
			complete: function(res) {
				console.log(res, "complete22")
			},
		})
	})
}
// 请求登录接口
export function handlecaptchalogin(username, captcha) {
	return new Promise((resolve, reject) => {
		uni.request({
			// url: `${API_URL}/anonym/userauth/login/`,
			// url: `${API_URL}/anonym/userauth/login/`,
			url: `${API_URL}/anonym/token/`,
			timeout: 10000,
			method: 'POST',
			data: {
				username: username,
				captcha: captcha,
			},
			responseType: 'application/json',
			header: {
				"Content-Type": "application/json",
				// "Access-Control-Allow-Credentials":true,
				// "Authorization":"JWT "+token.refresh,
				// "Authorization":"JWT "+token.access,
				// "Authentication" :token.refresh,
			},
			//注意这个地方要开启 true才会自动获取cookie和自动传递cookie
			// withCredentials:true,
			success: function(res) {
				console.log(res,'ressuccess');
				if(res.statusCode==200){
					uni.showToast({
						title: '登录成功！',
						icon: 'success'
					})
					// 保存请求头
					// uni.setStorageSync('JWTtoken', res.data.access)
					// uni.setStorageSync('JWTtoken—refresh', res.data.refresh)
					// 跳转到首页
					wx.switchTab({
						url: '/pages/student/index/index', // 替换为你的 tabBar 页面路径
						success: function(res) {
							// 跳转成功的回调
							console.log(res, '跳转成功')
						},
						fail: function(err) {
							// 错误处理
							console.log(err, '为什么不跳转')
						}
					}); 
				}else{
					uni.showToast({
						title: res.data.detail,
						icon: 'none'
					})
				}
				
				
			},
			fail: function(error) {
				showTishi(error)
				reject(error)
			},
			complete: function(res) {
				console.log(res, "complete22")
			},
		})
	})
}
// 获取验证接口
export function POSTcaptcha(phonenum) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${API_URL}/anonym/userauth/smscaptcha/`,
			timeout: 10000,
			method: 'POST',
			data: {
				phonenum: phonenum,
			},
			responseType: 'application/json',
			header: {
				"Content-Type": "application/json",
				// 'Cookie': ''
			},
			//注意这个地方要开启 true才会自动获取cookie和自动传递cookie
			withCredentials:true,
			success: function(res) {
				if(res.statusCode==200){
					showTishi('已发送验证码')
					return 1;
				}else{
					return 0;
				}
				
				
			},
			fail: function(error) {
				showTishi(error)
				reject(error)
			},
			complete: function(res) {
				console.log(res, "complete22")
			},
		})
	})
}


export function wxLogin() {
	wx.login({
		success: function(res) {
			if (res.code) {
				// handleLogin(res.code, true)
			}
			console.log('登录成功', res)
		},
		fail: function(res) {
			console.log('登录失败', res)
		},
		complete: function(res) {
			console.log('登录完成', res)
		}
	})

	wx.getUserInfo({
		success: function(res) {
			console.log('获取用户信息成功', res)
		},
		fail: function(res) {
			console.log('获取用户信息失败', res)
		},
		complete: function(res) {
			console.log('获取用户信息完成', res)
		}
	})
}